package com.kaiyidiannao.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.FocusOnBean;
import com.kaiyidiannao.model.bean.TourBean;
import com.kaiyidiannao.model.req.model.PostDeleteMessageInfo;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by david on 14-4-22.
 * 凑队去
 */
public class FocusOnAdapter extends ImageLoaderBaseAdapter{
    private ImageLoader imageLoader;
    private LayoutInflater layoutInflater;
    private List<?> list = new ArrayList<FocusOnBean>();
    private ArrayList<FocusOnBean> focusOnBeans = DataCenter.focusOnBeans;
    private String userid;
    private Context context;

    public FocusOnAdapter(Context context, List<?> list) {
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.list = list;
        this.layoutInflater =LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView imageView = null;
        TextView nameTextView = null;
        TextView  introduceTextView = null;
        FocusOnBean bean = null;
        ImageView imageView1 = null;
        LinearLayout focus ;
        TextView addtime;
        TextView adddate;
        TextView tour_line_popularity;
        TextView tour_line_hits;
        ImageButton btDelete ;
        try {
            bean = (FocusOnBean) list.get(position);
            if (convertView==null) {
                convertView=layoutInflater.inflate(R.layout.tour_line_item_,null);
            }
            imageView1 = (ImageView) convertView.findViewById(R.id.imageView);
            btDelete = (ImageButton) convertView.findViewById(R.id.btDelete);
            tour_line_hits = (TextView) convertView.findViewById(R.id.tour_line_hits);
            tour_line_popularity = (TextView) convertView.findViewById(R.id.tour_line_popularity);
            focus = (LinearLayout) convertView.findViewById(R.id.focus_on);
            adddate = (TextView) convertView.findViewById(R.id.adddate);
            addtime = (TextView) convertView.findViewById(R.id.addtime);
            imageView = (ImageView) convertView.findViewById(R.id.tour_line_imageview);
            nameTextView = (TextView) convertView.findViewById(R.id.tour_line_name);
            tour_line_popularity.setVisibility(View.GONE);
            tour_line_hits.setVisibility(View.GONE);
            introduceTextView = (TextView) convertView.findViewById(R.id.tour_line_introduce);
            focus.setVisibility(View.VISIBLE);
            if(DataCenter.backgroundTipBt==1){
                btDelete.setVisibility(View.VISIBLE);
            }else {
                btDelete.setVisibility(View.GONE);
            }
            introduceTextView.setVisibility(View.GONE);
            btDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FocusOnBean bean = focusOnBeans.get(position);
                    String action = "delete";
                    DataCenter.position = position;
                    if(DataCenter.userLogin.size()!=0){
                        userid = DataCenter.userLogin.get(0).getUserid();
                        ((BaseActionBarActivity)context).showProgressDialog();
                        new PostDeleteMessageInfo(bean.getFid(),userid,action).execute();
                    }
                }
            });
            imageView1.setVisibility(View.GONE);
            imageLoader.displayImage(bean.getThumb(),imageView,options,null);
            nameTextView.setText(bean.getTitle());
            adddate.setVisibility(View.GONE);
            addtime.setVisibility(View.GONE);
//            adddate.setText(bean.getAdddate()+"-");
//            addtime.setText(bean.getAdddate());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }
}
